package controller

import (
	"backend/dao/mysql"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

// OperationHistoryListView 操作历史列表接口
func OperationHistoryListView(c *gin.Context) {
	starttime := c.DefaultQuery("starttime", "")
	endtime := c.DefaultQuery("endtime", "")
	limit := c.DefaultQuery("limit", "")
	offset := c.DefaultQuery("offset", "")

	queryParams := map[string]string{
		"starttime": starttime,
		"endtime":   endtime,
		"limit":     limit,
		"offset":    offset,
	}

	operateHistoryList, err := mysql.GetOperateHistoryList(queryParams)
	if err != nil {
		zap.L().Error("获取数据失败：&u", zap.Error(err))
		ResponseError(c, CodeServerBusy)
	}
	ResponseSuccess(c, operateHistoryList)
}

// OperationHistoryView 根据路径参数id获取详情
func OperationHistoryView(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	operateHistory, err := mysql.GetOperateHistoryById(id)
	if err != nil {
		zap.L().Error("获取数据失败：", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, operateHistory)
}

// OperationHistoryDelView 根据路径参数id删除记录
func OperationHistoryDelView(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	_, err := mysql.DelOperateHistoryById(id)
	if err != nil {
		zap.L().Error("获取数据失败：", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, nil)
}

// TODO 新增/修改接口需要校验
